package org.erepublik.company.controllers;

import java.util.ArrayList;

import org.erepublik.company.log.SitesLogger;
import org.erepublik.company.product.Ticket;
import org.erepublik.company.searchCriteria.EAllianceSearchCriteria;
import org.erepublik.company.searchCriteria.TicketSearchCriteria;
import org.erepublik.company.service.IProductsService;
import org.erepublik.company.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/tickets.html")
public class TicketController extends AbstractController {


    @Autowired // autumatycznie wstrzykiwany obiekt
    private SitesLogger sitesLogger; 

    @Autowired // autumatycznie wstrzykiwany obiekt
    private IProductsService productService; 
    
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap model) {
        fillModelMap(model);
        Ticket[] products = productService.getTickets(null, 1);
        products = shrinkTable(products);
        model.addAttribute("updateTime", productService.getMinutesSinceTicketUpdate() );
        model.addAttribute("products", products);
        model.addAttribute("ticketSearchCriteria", new TicketSearchCriteria());
        sitesLogger.log("TICKETS", "Country: null Moving distance: true");
        return "tickets";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String showList(ModelMap model, @ModelAttribute("ticketSearchCriteria") TicketSearchCriteria criteria) {
        fillModelMap(model);
        Ticket[] products = productService.getTickets(criteria.getCountry(), criteria.getMovingDistance());
        products = filterAlliances(criteria.getAlliance(), products);
        products = shrinkTable(products);
        model.addAttribute("updateTime", productService.getMinutesSinceTicketUpdate() );
        model.addAttribute("products", products);
        sitesLogger.log("TICKETS", "Country: " + criteria.getCountry() + " Moving distance: "+criteria.getMovingDistance());
        return "tickets";
    }
    
    
    final int MAX_SIZE=50; // maksymalna ilosc wyswietlanych produktow
    // skraca tabele do MAX_SIZE produktow
    private Ticket[] shrinkTable(Ticket[] products) {
        if (products.length <= MAX_SIZE)
            return products;
        else {
            Ticket[] newTable = new Ticket[MAX_SIZE];
            for (int i=0; i < MAX_SIZE; i++) {
                newTable[i]=products[i];
            }
            return newTable;
        }
    }
    
    
    
    private Ticket[] filterAlliances(EAllianceSearchCriteria alliance, Ticket[] products) {

        ArrayList<Ticket> list = new ArrayList<Ticket>();
        for (Ticket food : products) {
            if ( Utils.contains( food.getCountry().getAlliance(), alliance.getAlliances() ) )
                list.add(food);
        }
        return list.toArray(new Ticket[0]);
    }
    
    
    
    
    
    
}
